<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<%@include file="../base/frameBase.jsp" %>
<body>
<div class="add_page">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>编辑文章</legend>
    </fieldset>
    <form class="layui-form" action="" id="createForm">
        <div class="layui-form-item">
            <label class="layui-form-label">文章标题</label>
            <div class="layui-input-block">
                <input type="text" name="page_title" lay-verify="title" autocomplete="off" placeholder="请输入课程标题"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">文章内容</label>
            <div class="layui-input-block">
                <div id="editor">
                    <p>编写文章的主题内容</p>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="confim">发布文章</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
        <hr>
    </form>

</div>

<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit;
        var $ = layui.jquery;

        var E = window.wangEditor
        var editor2 = new E('#editor')
        editor2.create()

        $.ajax({
            url: '${ctx}/videoData/getCategory',
            method: 'GET',
            contentType: 'application/json',
            success: function (res) {
                console.log(res);
                // 渲染课程分类菜单
                var category = res.data;
                var option = '';
                $.each(category, function (id, value) {
                    option += '<option value="' + value.id + '">' + value.category + '</option>';
                    console.log(option);
                })
                $("#category").html(option);
                form.render();
            },
            error: function (res) {
                console.log(res);
                alert(res.data);
            }
        })

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 1) {
                    return '标题至少得写点什么啊';
                }
            },
            content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听提交
        form.on('submit(confim)', function (data) {

            // 增加结果字段
            var page = editor2.txt.html();

            var note = editor2.txt.text();
            data.field['page_context'] = page;
            data.field['note'] = note;
            $.ajax({
                url: '${ctx}/pageData/addPage',
                method: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(data.field),
                success: function (res) {
                    console.log(res);
                    if (res.code == "00000") {

                        layer.msg("文章发布成功")
                        $(':input', '#createForm')
                            .not(':button, :submit, :reset, :hidden')
                            .val('')
                            .removeAttr('checked')
                            .removeAttr('selected');
                        editor2.txt.clear()
                    }else {
                        layer.msg(res.msg)
                    }
                },
                error: function (res) {
                    alert(res.data);
                }
            })
            return false;
        });
    });
</script>
</body>
<%@include file="../base/frameFoot.jsp" %>